<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="姓名或账户"
                v-model="listQuery.name"></el-input>
      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" v-if="informationManager_btn_add" style="margin-left: 10px;" @click="handleCreate"
                 type="primary" icon="edit">添加
      </el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row
              style="width: 100%">

      <el-table-column align="center" label="id" width="65">
        <template scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="项目编码">
        <template scope="scope">
          <span>{{scope.row.projectCode}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="项目名称">
        <template scope="scope">
          <span>{{scope.row.projectName}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="项目类型">
        <template scope="scope">
          <span>{{scope.row.projectType}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="项目描述">
        <template scope="scope">
          <span>{{scope.row.projectDescription}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="项目报价">
        <template scope="scope">
          <span>{{scope.row.projectQuotation}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="业主部门">
        <template scope="scope">
          <span>{{scope.row.ownerDepartment}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="合同编码">
        <template scope="scope">
          <span>{{scope.row.contractCode}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="合同时间">
        <template scope="scope">
          <span>{{scope.row.contractDate}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="合同金额">
        <template scope="scope">
          <span>{{scope.row.contractPrice}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="主管部门">
        <template scope="scope">
          <span>{{scope.row.competentAuthority}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="负责人ID">
        <template scope="scope">
          <span>{{scope.row.userId}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="项目负责人">
        <template scope="scope">
          <span>{{scope.row.projectManager}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="绩效核算主体">
        <template scope="scope">
          <span>{{scope.row.subject}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="绩效核算方式">
        <template scope="scope">
          <span>{{scope.row.way}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="绩效核算比例">
        <template scope="scope">
          <span>{{scope.row.ratio}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="创建时间">
        <template scope="scope">
          <span>{{scope.row.crtTime}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="创建人">
        <template scope="scope">
          <span>{{scope.row.crtName}}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" width="150">
        <template scope="scope">
          <el-button v-if="informationManager_btn_edit" size="small" type="success" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-if="informationManager_btn_del" size="small" type="danger" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="项目编码" prop="projectCode">
          <el-input v-model="form.projectCode" placeholder="请输入项目编码"></el-input>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称"></el-input>
        </el-form-item>
        <el-form-item label="项目类别" prop="projectType">
          <multiselect v-model="form.projectType" :options="projectTypeOptions" placeholder="请选择项目类型"></multiselect>
        </el-form-item>
        <el-form-item label="项目描述" prop="projectDescription">
          <el-input v-model="form.projectDescription" placeholder="请输入项目描述"></el-input>
        </el-form-item>
        <el-form-item label="项目报价" prop="projectQuotation">
          <el-input v-model="form.projectQuotation" placeholder="请输入项目报价"></el-input>
        </el-form-item>
        <el-form-item label="业主部门" prop="ownerDepartment">
          <el-input v-model="form.ownerDepartment" placeholder="请输入业主部门"></el-input>
        </el-form-item>
        <el-form-item label="合同编码" prop="contractCode">
          <el-input v-model="form.contractCode" placeholder="请输入合同编码"></el-input>
        </el-form-item>
        <el-form-item label="合同时间" prop="contractDate">
          <span class="demonstration">使用 value-format</span>
          <div class="demonstration">值：{{ form.contractDate }}</div>
          <el-date-picker
            v-model="form.contractDate"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
          <el-date-picker v-model="form.contractDate" align="right" type="date" format="yyyy-MM-dd"
                          value-format="yyyy-MM-dd" placeholder="选择合同时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="合同金额" prop="contractPrice">
          <el-input v-model="form.contractPrice" placeholder="请输入合同金额"></el-input>
        </el-form-item>
        <el-form-item label="主管部门" prop="competentAuthority">
          <el-input v-model="form.competentAuthority" placeholder="请输入主管部门"></el-input>
        </el-form-item>
        <el-form-item label="负责人ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入负责人ID"></el-input>
        </el-form-item>
        <el-form-item label="项目负责人" prop="projectManager">
          <el-input v-model="form.projectManager" placeholder="请输入项目负责人"></el-input>
        </el-form-item>
        <el-form-item label="绩效核算主体" prop="subject">
          <el-input v-model="form.subject" placeholder="请输入绩效核算主体"></el-input>
        </el-form-item>
        <el-form-item label="绩效核算方式" prop="way">
          <el-input v-model="form.way" placeholder="请输入绩效核算方式"></el-input>
        </el-form-item>
        <el-form-item label="绩效核算比例" prop="ratio">
          <el-input v-model="form.ratio" placeholder="请输入绩效核算比例"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {mapGetters} from 'vuex';
  import {addObj, delObj, getObj, page, putObj} from 'api/project/information/index';

  export default {
    name: 'information',
    data() {
      return {
        form: {
          projectCode: undefined,
          projectName: undefined,
          projectType: undefined,
          projectDescription: undefined,
          projectQuotation: undefined,
          ownerDepartment: undefined,
          contractCode: undefined,
          contractDate: undefined,
          contractPrice: undefined,
          competentAuthority: undefined,
          userId: undefined,
          projectManager: undefined,
          subject: undefined,
          way: undefined,
          ratio: undefined,
          status: undefined,
          remark: undefined,
          crtTime: undefined,
          crtUser: undefined,
          crtName: undefined,
          crtHost: undefined,
          updTime: undefined,
          updUser: undefined,
          updName: undefined,
          updHost: undefined
        },
        rules: {
          projectCode: [
            {
              required: true,
              message: '请输入项目编码',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], projectName: [
            {
              required: true,
              message: '请输入项目名称',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], projectType: [
            {
              required: true,
              message: '请输入项目类型'
            }
          ], projectDescription: [
            {
              required: true,
              message: '请输入项目描述',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], projectQuotation: [
            {
              required: true,
              message: '请输入项目报价',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], ownerDepartment: [
            {
              required: true,
              message: '请输入业主部门',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], contractCode: [
            {
              required: true,
              message: '请输入合同编码',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], contractPrice: [
            {
              required: false,
              message: '请输入合同金额',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], competentAuthority: [
            {
              required: false,
              message: '请输入主管部门',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], userId: [
            {
              required: false,
              message: '请输入负责人ID',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], projectManager: [
            {
              required: false,
              message: '请输入项目负责人',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], subject: [
            {
              required: false,
              message: '请输入绩效核算主体',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], way: [
            {
              required: false,
              message: '请输入绩效核算方式',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], ratio: [
            {
              required: false,
              message: '请输入绩效核算比例',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ], remark: [
            {
              required: false,
              message: '请输入备注',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 500,
              message: '长度在 0 到 500 个字符',
              trigger: 'blur'
            }
          ]
        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          name: undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        projectTypeOptions: ['维修单', '维修报告', '工程合同项目', '软件合同项目'],
        informationManager_btn_edit: false,
        informationManager_btn_del: false,
        informationManager_btn_add: false,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0
      }
    },
    created() {
      this.getList();
      this.informationManager_btn_edit = this.elements['informationManager:btn_edit'];
      this.informationManager_btn_del = this.elements['informationManager:btn_del'];
      this.informationManager_btn_add = this.elements['informationManager:btn_add'];
    },
    computed: {
      ...mapGetters([
        'elements'
      ])
    },
    methods: {
      getList() {
        this.listLoading = true;
        page(this.listQuery)
          .then(response => {
            this.list = response.data.rows;
            this.total = response.data.total;
            this.listLoading = false;
          })
      },
      handleFilter() {
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      handleCreate() {
        this.resetTemp();
        this.dialogStatus = 'create';
        this.dialogFormVisible = true;
      },
      handleUpdate(row) {
        getObj(row.id)
          .then(response => {
            this.form = response.data;
            this.dialogFormVisible = true;
            this.dialogStatus = 'update';
          });
      },
      handleDelete(row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            delObj(row.id)
              .then(() => {
                this.$notify({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                });
                const index = this.list.indexOf(row);
                this.list.splice(index, 1);
              });
          });
      },
      create(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            addObj(this.form)
              .then(() => {
                this.dialogFormVisible = false;
                this.getList();
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                });
              })
          } else {
            return false;
          }
        });
      },
      cancel(formName) {
        this.dialogFormVisible = false;
        const set = this.$refs;
        set[formName].resetFields();
      },
      update(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            this.dialogFormVisible = false;
            this.form.password = undefined;
            putObj(this.form.id, this.form).then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              });
            });
          } else {
            return false;
          }
        });
      },
      resetTemp() {
        this.form = {
          username: undefined,
          name: undefined,
          sex: '男',
          password: undefined,
          description: undefined
        };
      }
    }
  }
</script>
